package uestc.lj.gulishop.product.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import uestc.lj.gulishop.product.entity.CategoryEntity;
import uestc.lj.gulishop.product.service.CategoryService;
import uestc.lj.gulishop.product.vo.Catelog2VO;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class IndexController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 显示首页，将所有一级分类封装进model返回
     *
     * @param model
     * @return
     */
    @GetMapping({"/", "/index.html"})
    public String index(Model model) {
        //优化数据库查询
        // 1.查出所有的1级分类
        List<CategoryEntity> categoryEntityList = categoryService.getLevelOneCategory();
        model.addAttribute("categorys", categoryEntityList);
        return "index";
    }

    /**
     * 查询一级分类下的所属菜单
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/index/catalog.json")
    public Map<String, List<Catelog2VO>> getCatalogJson() {
        return categoryService.getCatalogJson();
    }
}
